package com.weixiaodong.ModelDesign.生成实例.Singleton模式.练习.计数器;

@SuppressWarnings("all")
public class TicketMaker {

    private volatile int ticket = 1000;

    private static volatile TicketMaker ticketMaker;

    private TicketMaker() {};

    public static TicketMaker getInstance() {
        if (ticketMaker == null) {
            synchronized (TicketMaker.class) {
                if (ticketMaker == null) {
                    ticketMaker = new TicketMaker();
                }
            }
        }
        return ticketMaker;
    }

    public int getNextTicketNumber() {
        synchronized (TicketMaker.class) {
            return ticket++;
        }
    }

    public int getTicket() {
        return ticket;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            System.out.println("id: " + TicketMaker.getInstance().getNextTicketNumber());
        }
    }
}
